package com.hula.onion.book.server.spider.platforms.zhubaodai;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/3/19.
 */
@Component
public class ZhuBaoDaiSpiderHelper {

    public Map<String, Object> installRequestParams(LoginForm form) {
        Map<String, Object> result = new HashMap<>(4);
        result.put("CI_NM", form.getUserName());
        result.put("RG_PWD", DigestUtils.md5Hex(form.getPassword()));
        result.put("isRemember", "0");
        result.put("DF_PHN", "0");
        return result;
    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {
        try {
            Document document = Jsoup.parse(html);
            String amount = document.selectFirst("#dpsTotAmt").text();
            String waitInterest = document.selectFirst("#dpsIntAmt").text();
            String totalInterest = document.selectFirst("div.p2pcg-info.asset_info_b.fn-clear").
                    children().get(0).children().get(0).text();
            String fTI = StringUtils.remove(totalInterest, "元");
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setAmount(new BigDecimal(amount));
            accountInfo.setWaitIntrest(new BigDecimal(waitInterest));
            accountInfo.setTotalIntrest(new BigDecimal(fTI));
            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }
}
